var contactList = [];

function init() {
	_initHeader();
	
	$("#searchButton").unbind("click");
	$("#addButton").unbind("click");
	$("#searchButton").click(clickedSearchButton);
	$("#addButton").click(clickedAddButton);
	
	commonProgress("Searching Contacts..", false, null, "grey");
	
	contact.search(null, searchSuccessCallback, searchErrorCallback);
}

function clickedAddButton() {
	navigate("contact_add");
}

function clickedSearchButton() {
	var filter = {};
	var selectedMenu = $("#selectMenu option:selected").val();
	var searchWord = $("#searchInput").val();
	
	if($("#selectMenu option:selected").val() == "all") {
		if(searchWord == "") {
			commonProgress("Searching Contacts..", false, null, "grey");
			contact.search(null, searchSuccessCallback, searchErrorCallback);
			
			return;
		} else {
			filter.firstName = searchWord;
			filter.lastName = searchWord;
			filter.mobilePhone = searchWord;
			filter.workPhone = searchWord;
			filter.email = searchWord;
			filter.organization = searchWord;
			filter.department = searchWord;
			filter.jobTitle = searchWord;
		}
	} else {
		if(searchWord == null || searchWord == "" || searchWord == undefined) {
			alert("검색어를 입력하세요.");
			return;
		}
		
		if(selectedMenu == "firstName")
			filter.firstName = searchWord;
		else if(selectedMenu == "lastName")
			filter.lastName = searchWord;
		else if(selectedMenu == "mobilePhone")
			filter.mobilePhone = searchWord;
		else if(selectedMenu == "workPhone")
			filter.workPhone = searchWord;
		else if(selectedMenu == "email")
			filter.email = searchWord;
		else if(selectedMenu == "organization")
			filter.organization = searchWord;
		else if(selectedMenu == "department")
			filter.department = searchWord;
		else if(selectedMenu == "jobTitle")
			filter.jobTitle = searchWord;
	}
	var searchOption = {
		"filter" : filter,
		"andOption" : false
	};
	
	commonProgress("Searching Contacts..", false, null, "grey");
	contact.search(searchOption, searchSuccessCallback, searchErrorCallback);
}

function searchSuccessCallback(param) {
	contactList = param;
	var outputHtml = "";
	
	if(param.length > 0) {
		document.getElementById("listview").innerHTML = "";
		
		var listData = [];
		
		for( var i = 0; i < contactList.length; i++) {
			if(contactList[i].firstName == undefined)
				contactList[i].firstName = "";
			
			if(contactList[i].lastName == undefined)
				contactList[i].lastName = "";
			
			listData[i] = contactList[i].lastName + " " + contactList[i].firstName;
		}
		
		$.each(listData, function(idx, value) {
			outputHtml = "<li id=\"contact_" + idx + "\"><a href=\"#\">" + value + "</a></li>";
			$("#listview").append(outputHtml);
			$("#contact_" + idx).click(function(e) {
				clickedListItem(idx);
			});
		});
	} else {
		document.getElementById("listview").innerHTML = "";
		outputHtml = "<li disabled= \"disabled\">No contact.</li>";
		$("#listview").append(outputHtml);
	}
	
	$("#listview").listview("refresh");
	platformUIComponent.dismissProgressDialog();
}

function clickedListItem(idx) {
	var navigationRule = {
		"pageId" : "contact_detail",
		"parameters" : {
			"contactId" : contactList[idx].contactId
		}
	};
	
	navigation.navigate(navigationRule);
}

function searchErrorCallback(errorMessage) {
	notifyError("search", errorMessage);
}

function onScreenBack() {
	init();
	log.log("onScreenBack on conatct page is called.");
}
